import { ajaxAsyncRN } from 'rn-xiaobu-utils'
import { api, token } from '../configs/api'
import NavigationService from './NavigationService'

/**
 *
 * @param url
 * @param sendObj
 * @param autoExLvl
 * @param selfLogin 自动拉起登录参数
 * @returns {Promise<Object | never>}
 */
async function ajaxAsync (url, sendObj, autoExLvl = 0, autoLogin = true) {
  // await ajaxAsyncRN(api, token, url, sign(JSON.stringify(sendObj), token, ''), '', 3000, autoExLvl)
  // let sessionID = ''
  // try {
  //     const sessionIDCopy = await AsyncStorage.getItem('XIAOBUSESSION')
  //     if (sessionIDCopy) {
  //         sessionID = sessionIDCopy.trim()
  //     }
  // } catch (e) {
  //     console.log('获取sessionID异常')
  // }
  //
  // try {
  //     return await ajaxAsyncRN(api, token, url, sign(JSON.stringify(sendObj), token, sessionID), sessionID, 3000, autoExLvl)
  // } catch (e) {
  //     if (e.needLogin) {
  //         console.log('需要登录')
  //     }
  //     if (e.toastMsg) {
  //         console.log('需要toast')
  //         ToastAndroid.show(e.toastMsg, ToastAndroid.SHORT)
  //     }
  //     throw e.error
  // }

  return ajaxAsyncRN(api, token, url, sendObj, 3000, autoExLvl).catch(error => {
    if (autoLogin && error.needLogin) {
      NavigationService.navigate('Login', { backtoroot: true })
    } else {
      throw error
    }
  })
}

export default ajaxAsync